# /usr/bin/env python3.6
# -*- encoding: utf-8 -*-
"""
@File    : cinema_hall_movie_api.py
@Time    : 2020/2/8 0008 13:29
@Author  : zhangbing
@Email   : 18829172841@163.com
@Software: PyCharm
"""
import datetime

from flask import g

from App.apis.api_constant import HTTP_CREATE_OK
from App.apis.cinema_admin.utils import login_cinema_required
from App.models.cinema_admin.cinema_address_model import CinemaAddress
from App.models.cinema_admin.cinema_hall_model import Hall
from App.models.cinema_admin.cinema_movie_model import CinemaMovie
from App.models.cinema_admin.cinma_hall_movie_model import HallMovie

'''
此接口主要是处理排挡相关的操作，其中必须是该电影用户才能操作的
'''
from flask_restful import Resource, reqparse, abort, fields, marshal

'''
    h_movie_id = db.Column(db.Integer, db.ForeignKey(Movie.id))
    h_hall_id = db.Column(db.Integer, db.ForeignKey(Hall.id))
    h_time = db.Column(db.DateTime)
'''

parse = reqparse.RequestParser()

parse.add_argument("h_movie_id", type=int, required=True, help="Plase  input movie id ")
parse.add_argument("h_hall_id", type=int, required=True, help="Plase  input hall id ")
parse.add_argument("h_time", required=True, help="Plase  input datatime")

moviehall = {
    "h_movie_id": fields.Integer,
    "h_hall_id": fields.Integer,
    "h_time": fields.DateTime
}


class CinemaMovieHallsReource(Resource):
    def get(self):
        return {"msg": "ok"}

    @login_cinema_required
    def post(self):
        # 此处可以获取其user_id ，通过user_id来查看其电影进而查看其影厅，进而确定该电影和影厅是否是该用户下的，进而才能进行相关的排挡操作
        args = parse.parse_args()
        h_movie_id = args.get("h_movie_id")
        print(g.user.id)
        if not CinemaMovie.query.filter(CinemaMovie.c_cinema_id == g.user.id).first():  # 判断该用户是否存在电影
            abort(404, msg="该影片不存在")
        h_hall_id = args.get("h_hall_id")
        h_time = args.get("h_time")
        hallmovie = HallMovie()
        hallmovie.h_hall_id = h_hall_id
        hallmovie.h_movie_id = h_movie_id
        hallmovie.h_time = h_time
        if not hallmovie.save():
            abort(403, msg="电影排挡失败")

        data = {
            "msg": "排挡成功！！！",
            "status": HTTP_CREATE_OK,
            "data": marshal(hallmovie, moviehall)
        }
        return data


if __name__ == "__main__":
    pass
